//
//  CardDetailsViewController.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/9/5.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit

class CardDetailsViewController: KMViewController {
    
    var keyArray = [String]()
    fileprivate var detailView : CardDetailsView!
    
    init(keyArray:[String],title:String) {
        super.init(nibName: nil, bundle: nil)
        
        self.keyArray = keyArray
        self.title = title
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        addSubView()
        
    }
    
    //MARK: 添加子视图
    fileprivate func addSubView() {
        
        detailView = CardDetailsView.init(frame: self.view.bounds)
        detailView.detailsButton.addTarget(self, action: #selector(viewDetails), for: .touchUpInside)
        detailView.freeButton.addTarget(self, action: #selector(freeApplication), for: .touchUpInside)
        detailView.backButton.addTarget(self, action: #selector(backAction), for: .touchUpInside)
        self.view.addSubview(detailView)
        
        getData()
    }
    
    //MARK: 获取数据
    fileprivate func getData() {
        
        KMTool.waiting()
        KMTool.requestMethod(.get, URLString: url_cardArtifact, parameters: ["key1":keyArray[0],"key2":keyArray[1]], success: { (value, json) in
            
            KMTool.stop()
            let model = CardDetailsModel.setModel(with: json.dictionaryObject!)
            self.detailView.model = model
            
            self.detailView.typeLabel.text = characteristics + "：" + "[" + self.keyArray[0] + "]" + "[" + self.keyArray[1] + "]"
            
        }) {
            KMTool.stop()
        }
    }
    
    //MARK: 重新计算
    @objc fileprivate func backAction() {
        
        self.navigationController?.popViewController(animated: true)
    }
    
    //MARK: 免费申请
    @objc fileprivate func freeApplication() {
        
        var urlString = ""
        
        if let model = self.detailView.model {
            if model.app_apply_access != "" {
                urlString = model.app_apply_access!
            }else if model.m_apply_access != "" {
                urlString = model.m_apply_access!
            }else if model.apply_access != "" {
                urlString  = model.apply_access!
            }
        }
        
        if urlString == "" {
            return
        }
        
        urlString = urlString.replacingOccurrences(of: "http", with: "https")
        
        KMTool.generateRecords(idString: self.detailView.model.id!)
        
        let webVC = WebViewController.init(urlString: urlString, title: self.title)
        webVC.hidesBottomBarWhenPushed = true
        self.navigationController?.pushViewController(webVC, animated: true)
        
    }
    
    //MARK: 查看详情
    @objc fileprivate func viewDetails() {
        
        if let id = self.detailView.model.id {
            //详情链接
            let urlString = "https://m.kameng98.com/mobile/details/i"
            let webVC = WebViewController.init(urlString: urlString + id, title: self.title)
            webVC.hidesBottomBarWhenPushed = true
            self.navigationController?.pushViewController(webVC, animated: true)
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        
        
    }


}
